package br.edu.ufcg.dsc.actions.resource;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Vector;

import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;

import br.edu.ufcg.dsc.domain.ResourceType;
import br.edu.ufcg.dsc.domain.marc.DataField;
import br.edu.ufcg.dsc.domain.marc.MARCResource;
import br.edu.ufcg.dsc.domain.resource.Article;
import br.edu.ufcg.dsc.domain.resource.ArticlesCollection;
import br.edu.ufcg.dsc.domain.resource.Book;
import br.edu.ufcg.dsc.domain.resource.Issue;
import br.edu.ufcg.dsc.domain.resource.Serial;
import br.edu.ufcg.dsc.domain.resource.TechnicalReport;
import br.edu.ufcg.dsc.domain.resource.Thesis;
import br.edu.ufcg.dsc.domain.resource.Volume;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Results(value = {
		@Result(name = "ARTICLE", type = ServletActionRedirectResult.class, value = "findArticle.action", params={"namespace", "resource"}),
		@Result(name = "BOOK", type = ServletActionRedirectResult.class, value = "findBook.action", params={"namespace", "resource"}),
		@Result(name = "SERIAL", type = ServletActionRedirectResult.class, value = "findSerial.action", params={"namespace", "resource"}),
		@Result(name = "ISSUE", type = ServletActionRedirectResult.class, value = "findIssue.action", params={"namespace", "resource"}),
		@Result(name = "ARTICLES_COLLECTION", type = ServletActionRedirectResult.class, value = "findArticlesCollection.action", params={"namespace", "resource"}),
		@Result(name = "VOLUME", type = ServletActionRedirectResult.class, value = "findVolume.action", params={"namespace", "resource"}),
		@Result(name = "THESIS", type = ServletActionRedirectResult.class, value = "findThesis.action", params={"namespace", "resource"}),
		@Result(name = "TECHNICAL_REPORT", type = ServletActionRedirectResult.class, value = "findTechnicalReport.action", params={"namespace", "resource"}) })
public class ChangeToSimpleAction extends ActionSupport {

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {

		// ALL, ARTICLE, BOOK, SERIAL, ISSUE, ARTICLES_COLLECTION, VOLUME,
		// THESIS, TECHNICAL_REPORT;

		MARCResource toCreate = new MARCResource();
		Map<String, String[]> m = ActionContext.getContext().getParameters();

		ResourceType type = ResourceType.valueOf(m.get("docType")[0]);

		Vector<String> keys = new Vector<String>();

		for (String key : m.keySet()) {
			if (key.contains("subfield")
					&& !"[]".equals(Arrays.toString(m.get(key)))) {
				keys.add(key);
			}
		}

		Collections.sort(keys);

		System.out.println("\n\n\n\n" + keys + "\n\n\n\n\n\n");
		
		int i;
		// Control fields;
		for (i = 0; i < keys.size(); i++) {
			String key = keys.get(i);

			if (!key.endsWith("_00"))
				break;

			String field = key.substring(4, 7);

			if (field.equals("000"))
				toCreate.setLeader(convert(m.get(key)));
			else
				toCreate.getControlFields().put(field, convert(m.get(key)));
		}

		// Data fields;
		String previousField = null;
		DataField datafield = null;

		for (; i < keys.size(); i++) {
			String key = keys.get(i);

			String field = key.substring(4, 7);
			String subField = key.substring(17);

			if (!field.equals(previousField)) {
				String indicator1 = "tag_" + field + "_indicator1";
				String indicator2 = "tag_" + field + "_indicator2";

				datafield = new DataField(tryToGet(m.get(indicator1)), tryToGet(m
						.get(indicator2)));
				toCreate.getDataFields().put(field, datafield);
			}

			datafield.getSubfields().put(subField, convertMultiple(m.get(key)));

			previousField = field;
		}

		Object result = null;

		switch (type) {
		case ARTICLE:
			result = new Article(toCreate);
		case BOOK:
			result = new Book(toCreate);
		case SERIAL:
			result = new Serial(toCreate);
		case ISSUE:
			result = new Issue(toCreate);
		case ARTICLES_COLLECTION:
			result = new ArticlesCollection(toCreate);
		case VOLUME:
			result = new Volume(toCreate);
		case THESIS:
			result = new Thesis(toCreate);
		case TECHNICAL_REPORT:
			result = new TechnicalReport(toCreate);
		}

		ActionContext.getContext().getSession().put("changeToSimpleResource",
				result);

		return type.name();
	}

	private String tryToGet(String[] strings) {
		if (strings == null)
			return "";
		return strings[0];
	}

	private String convertMultiple(String[] strings) {
		String s = Arrays.toString(strings);
		return s.substring(1, s.length() - 1);
	}

	private String convert(String[] strings) {
		return strings[0];
	}

}
